<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$storeid = intval($_GPC['storeid']);
$setting = $this->getSetting();
$cur_nave = 'my';
$status = 0;
$level = intval($_GPC['level']);
if (!empty($_GPC['status'])) {
    $status = intval($_GPC['status']);
}

$do = 'my';
if ($setting['auth_mode'] == 1 || empty($setting)) {
    $method = 'my'; //method
    $host = $this->getOAuthHost();
    $authurl = $host . 'app/' . $this->createMobileUrl($method, array(), true) . '&authkey=1';
    $url = $host . 'app/' . $this->createMobileUrl($method, array(), true);
    if (isset($_COOKIE[$this->_auth2_openid])) {
        $from_user = $_COOKIE[$this->_auth2_openid];
        $nickname = $_COOKIE[$this->_auth2_nickname];
        $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
    } else {
        if (isset($_GPC['code'])) {
            $userinfo = $this->oauth2($authurl);
            if (!empty($userinfo)) {
                $from_user = $userinfo["openid"];
                $nickname = $userinfo["nickname"];
                $headimgurl = $userinfo["headimgurl"];
            } else {
                message('授权失败!');
            }
        } else {
            if (!empty($this->_appsecret)) {
                $this->getCode($url);
            }
        }
    }
} else {
    load()->model('mc');
    if (empty($_W['fans']['nickname'])) {
        mc_oauth_userinfo();
    }
    $from_user = $_W['fans']['openid'];
    $nickname = $_W['fans']['nickname'];
    $headimgurl = $_W['fans']['tag']['avatar'];
}


if (empty($from_user)) {
    message('会话已过期，请重新发送关键字!');
}

$zero_time = mktime(0, 0, 0);
//今日金额
$today_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND dateline>:time
 AND status<>-1 AND ispay=1 AND ismerge=0 ", array(':weid' => $this->_weid, ':storeid' => $storeid, ':time' => $zero_time));
$today_order_price = sprintf('%.2f', $today_order_price);

$nowmonth_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND status<>-1 AND ispay=1 AND
ismerge=0 AND DATE_FORMAT(FROM_UNIXTIME(dateline),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m')", array(':weid' => $weid,":storeid" => $storeid));
$nowmonth_order_price = sprintf('%.2f', $nowmonth_order_price);

$lastmonth_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE
    weid=:weid AND storeid=:storeid AND status<>-1 AND ispay=1 AND
ismerge=0 AND PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(FROM_UNIXTIME(dateline), '%Y%m' ) ) =1",
    array(':weid' => $weid,":storeid" => $storeid));
$lastmonth_order_price = sprintf('%.2f', $lastmonth_order_price);


if ($level == 1) {
    $condition = " AND dateline>{$zero_time} ";
} elseif ($level == 2) {
    $condition = " AND DATE_FORMAT(FROM_UNIXTIME(dateline),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') ";
} elseif ($level == 3) {
    $condition = " AND PERIOD_DIFF(date_format(now(),'%Y%m'),date_format(FROM_UNIXTIME(dateline),'%Y%m'))=1 ";
}

//营业详情
$data = pdo_fetchall('SELECT * FROM ' . tablename($this->table_order) . " WHERE weid = :weid AND storeid = :storeid AND status<>-1 AND ispay=1 {$condition}
ORDER BY
dateline DESC LIMIT 1000", array(':weid' => $this->_weid, ':storeid' => $storeid));
$total = array();

$footer_totalprice = 0;
$footer_totalcount = 0;
$footer_totalprice1 = 0;
$footer_totalprice2 = 0;
$footer_totalprice3 = 0;
$footer_totalprice4 = 0;
if (!empty($data)) {
    foreach ($data as &$da) {
        $total_price = floatval($da['totalprice']);
        $totalprice = floatval($da['totalprice']);
        $sid = intval($da['storeid']);
        $store = pdo_fetch("SELECT * FROM " . tablename($this->table_stores) . " WHERE id=:id LIMIT 1;", array(':id'
            => $sid));
        if ($store['is_default_rate']==1) {
            $show_fee_rate = intval($setting['fee_rate']);
        } else {
            $show_fee_rate = intval($store['fee_rate']);
        }
        $fee= $totalprice * floatval($show_fee_rate)/100;
        $totalprice = $totalprice - $fee;

        $key = date('Y-m-d', $da['dateline']);
        if ($da['paytype'] != '3' && $da['paytype'] != '0') {
            $return[$key]['totalprice'] += $totalprice;
        }

        $footer_totalprice += $totalprice;
        $footer_totalcount += 1;
        $return[$key]['count'] += 1;
//        $total['total_price'] += $total_price;
//        $total['total_count'] += 1;

        if ($da['paytype'] == '1') {
            //余额
            $return[$key]['1'] += $total_price;
            $footer_totalprice1 += $total_price;
        } elseif ($da['paytype'] == '2') {
            //微信
            $return[$key]['2'] += $total_price;
            $footer_totalprice2 += $total_price;
        } elseif ($da['paytype'] == '4') {
            //支付宝
            $return[$key]['4'] += $total_price;
            $footer_totalprice4 += $total_price;
        } elseif ($da['paytype'] == '3' || $da['paytype'] == '0') {
            $return[$key]['3'] += $total_price;
            $footer_totalprice3 += $total_price;
        }
    }
}


include $this->template($this->cur_tpl . '/storestart');